package org.geometerplus.fbreader.fbreader;

import android.content.Context;
import android.text.TextUtils;

import com.chineseall.readbusiness.dialog.PayDialog;
import com.chineseall.reader.ui.util.BatchAndSingleDownload;
import com.chineseall.reader.ui.util.VolumeUtils;
import com.chineseall.readerapi.beans.ShelfItemBook;
import com.chineseall.readerapi.entity.Chapter;
import com.iwanvi.common.report.StaticsLogService;

import org.geometerplus.fbreader.formats.m17k.MTxtNovelReader;

import java.lang.ref.SoftReference;

/**
 * 点击购买
 * 单张和全书购买
 */

public class ReadBuyAction extends FBAction {
    public ReadBuyAction(FBReaderApp fbreader) {
        super(fbreader);
    }

    @Override
    protected void run(Object... params) {
        int payType = 0;
        String chapterId = "0";
        long price = 0;
        Chapter mReadingChapter = null;
        if (params.length > 0) {
            Object[] objects = (Object[]) params[2];
            if (objects != null) {
                payType = (int) objects[0];
                price = (long) objects[1];
                chapterId = (String) objects[2];
                mReadingChapter = (Chapter) objects[3];
            }
        }

        int payTypeCopy = 0;
        if (TextUtils.isEmpty(chapterId)) {
            chapterId = "0";
        }
        if (payType == 1) {
            payTypeCopy = 3;  //单章购买
        } else if (payType == 2) {
            payTypeCopy = 1;  //整本购买
        }
        sendLog(payTypeCopy);
        PayDialog dialog = PayDialog.newInstance(((MTxtNovelReader) Reader.getBookReader()).getBookId() + "", Integer.parseInt(chapterId), (int) price, payTypeCopy);
        dialog.show(Reader.getAppContext());
        dialog.setOnPayListener(new PayListener(Reader.getAppContext().getShelfItemBook(), mReadingChapter, payTypeCopy, dialog));
    }

    private void sendLog(int payTypeCopy) {
        String pfp = "";
        if (payTypeCopy == 3) {
            pfp = "1-1";
        } else {
            pfp = "1-3";
        }
        StaticsLogService.sendLog("3613", pfp);
    }

    private class PayListener implements PayDialog.OnPayListener {
        private Chapter mReadingChapter;
        private ShelfItemBook book;
        private PayDialog dialog;
        private int payType;

        private PayListener(ShelfItemBook book, Chapter mReadingChapter, int payType, PayDialog dialog) {
            this.mReadingChapter = mReadingChapter;
            this.book = book;
            this.payType = payType;
            this.dialog = dialog;
        }

        @Override
        public void onBuySuccess() {
            if (payType == 1) {//表示全本下载
                BatchAndSingleDownload.with(Reader.getAppContext()).batchTheBookDownLoad(book, mReadingChapter);
                dialog.getListener().paySuccess();
            } else {//表示下载单张
                BatchAndSingleDownload.with(Reader.getAppContext()).singleDownload(dialog.getListener(),book, mReadingChapter, mReadingChapter);

            }

            VolumeUtils.getInstance().updateLocalVolume(new SoftReference<Context>(Reader.getAppContext()), book.getBookId());
        }

        @Override
        public void onBuyError(String msg) {

        }
    }


}
